/*
 * Copyright 2008 aileron.cc, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.orm.logger;

import java.sql.Timestamp;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

/**
 * @author Aileron
 */
public class SqlLoggerImpl implements SqlLogger
{
    /**
     * sql のパラメータを置き換える為の正規表現
     */
    private static final Pattern pattern = Pattern.compile("\\?");

    @Override
    public boolean isEnabled()
    {
        return isEnabled;
    }

    /*
     * (非 Javadoc)
     * 
     * @see
     * cc.aileron.template.sql.impl.SqlLogInterface#output(cc.aileron.template
     * .sql.impl.SqlLoggerParameter)
     */
    public void output(final SqlLoggerParameter p)
    {
        if (p.arguments.size() != 0)
        {
            int i = 0;
            final StringBuffer buffer = new StringBuffer();
            final Matcher matcher = pattern.matcher(p.sql);
            for (final Object object : p.arguments)
            {
                if (matcher.find())
                {
                    if (object == null)
                    {
                        matcher.appendReplacement(buffer, "/*" + (i++)
                                + "*/null");
                        continue;
                    }
                    if (object.getClass()
                        .isPrimitive())
                    {
                        matcher.appendReplacement(buffer, getTypeComment(i++,
                                object)
                                + object.toString());
                        continue;
                    }
                    if (Date.class.isAssignableFrom(object.getClass()))
                    {
                        final String value = new Timestamp(
                                ((Date) object).getTime()).toString();
                        matcher.appendReplacement(buffer, getTypeComment(i++,
                                object)
                                + "'" + value + "'");
                        continue;
                    }
                    matcher.appendReplacement(buffer, getTypeComment(i++,
                            object)
                            + "'" + object.toString() + "'");
                }
            }
            matcher.appendTail(buffer);
            logger.debug(p.name + "=>" + buffer.toString());
        }
        else
        {
            logger.debug(p.name + "=>" + p.sql);
        }
    }

    /**
     * 型情報の取得
     * 
     * @param object
     * @return
     */
    private String getTypeComment(final int number,
            final Object object)
    {
        return "/*" + number + ":" + object.getClass()
            .getName() + "*/";
    }

    /**
     * 
     */
    @Inject
    public SqlLoggerImpl()
    {
        this.logger = LoggerFactory.getLogger(SqlLogger.class);
        this.isEnabled = this.logger.isDebugEnabled();
    }

    /**
     * is-debug
     */
    private final boolean isEnabled;

    /**
     * logger
     */
    private final Logger logger;
}